<?php
namespace app\base\controller;

use app\base\model\SubjectModel;
use app\common\base\BasicRest;
use think\Request;
use think\Session;

class Subject extends  BasicRest
{
    public function __construct()
    {
        parent::__construct();
    }

    #获取所有学科信息
    public function index(){
        $subjectData = SubjectModel::where('IsDeleted',0)->select();
        if($subjectData) return res_success($subjectData);
        return res_fail('未找到信息');
    }

    #获取单个学科信息
    public function read($id){
        $subjectData = SubjectModel::get($id);
        if($subjectData) return res_success($subjectData);
        return res_fail('未找到信息');
    }

    #新增学科信息
    public function save(Request $request){
        $res = SubjectModel::create($request->post());
        if($res) return res_success();
        return res_fail('创建失败');
    }

    #修改学科信息
    public function update(Request $request,$id){
        $data = $request->put();
        unset($data['CreateId']);
        unset($data['CreateTime']);
        unset($data['UpdateId']);
        unset($data['UpdateTime']);
        $model = SubjectModel::find($id);
        $res = $model->save($data);
        if($res!== false) return res_success();
        return res_fail('修改失败');
    }

    #删除学科信息
    public function delete($id){
        $res = SubjectModel::where('Id',$id)->update(['IsDeleted'=>'1']);
        if($res) return res_success();
        return res_fail('删除失败');
    }
}